<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/8/3
 * Time: 14:44
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Zhixu extends Base
{
    /**
     * 秩序单
     * @return mixed|\think\response\Json
     */
    public function index(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->field('match_id')
            ->find();
        $group = Db::table('up_match_apply_group')
            ->join('up_match_group', 'mg_id = mag_group_id', 'left')
            ->where('mag_match_id', $match['match_id'])
            ->field('mg_id, mg_name')
            ->select();
        $turns = Db::table('up__match_turns')
            ->field('mt_id, mt_name')
            ->select();
        if(Request::instance()->isAjax ()){
            $boat_type = input('boat_type'); // 船型
            $group_id = input('group_id'); //  组
            $distance = input('distance'); // 赛道距离
            $turns_id = input('turns_id'); // 轮次
            
            $where['ma_match_id'] = $match['match_id'];
            $where['ma_role'] = 1;
            // 组别
            if(isset($group_id) && $group_id != ''){
                $where['ma_group'] = $group_id;
            }
            /*else{
                $where['ma_group'] = $group[0]['mg_id'];
            }*/
            // 船型
            if(isset($boat_type) && $boat_type != ''){
                $where['ma_boat_type'] = $boat_type;
            }
            /*else{
                $where['ma_boat_type'] = 1;
            }*/
            // 赛道距离
            if(isset($distance) && $distance != ''){
                $where['ma_distance'] = $distance;
            }
            /*else{
                $where['ma_distance'] = 500;
            }*/
            // 轮次
            if(isset($turns_id) && $turns_id != ''){
                $where['mt_id'] = $turns_id;
            }
            /*else{
                $where['mt_id'] = $turns[0]['mt_id'];
            }*/
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('match_apply_info')
                ->join('up_club', 'ma_club_id = club_id', 'left')
                ->join('up_match_group', 'mg_id = ma_group', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->join('up__match_turns', 'mt_id = ma_turns_id', 'left')
                ->join('up__match_rounds', 'mr_id = ma_rounds_id', 'left')
                ->join('up_u_user', 'user_id = ma_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $count1 = Db::name('match_apply_info')
                ->join('up_club', 'ma_club_id = club_id', 'left')
                ->join('up_match_group', 'mg_id = ma_group', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->join('up__match_turns', 'mt_id = ma_turns_id', 'left')
                ->join('up__match_rounds', 'mr_id = ma_rounds_id', 'left')
                ->join('up_u_user', 'user_id = ma_user_id', 'left')
                ->where($where)
                ->column('ma_id');
            $lists = Db::name('match_apply_info')
                ->join('up_club', 'ma_club_id = club_id', 'left')
                ->join('up_match_group', 'mg_id = ma_group', 'left')
                ->join('up_match', 'ma_match_id = match_id', 'left')
                ->join('up__match_turns', 'mt_id = ma_turns_id', 'left')
                ->join('up__match_rounds', 'mr_id = ma_rounds_id', 'left')
                ->join('up_u_user', 'user_id = ma_user_id', 'left')
                ->field('ma_id, mg_name, ma_boat_type, match_start_time, ma_group_num, ma_dao_num, 
                ma_team_name, club_name, ma_match_id, mt_name, mr_name, user_phone, user_real_name')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('ma_time desc')
                ->select();
            foreach($lists as $k => $v){
                // 参赛队员
                $join_user = Db::table('up_match_apply_info')
                    ->where('ma_team_name', $v['ma_team_name'])
                    ->where('ma_match_id', $v['ma_match_id'])
                    ->column('ma_user_name');
                $lists[$k]['ma_user_name'] = implode(',', $join_user);
            }
            $str = '';
            if($count1){
                $str = implode($count1,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count1' => $str, 'data' => $lists]);
        }
        $this->assign([
            'group' => $group,
            'turns' => $turns
        ]);
        return $this->fetch();
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $ma_ids = input('ids');
        if($ma_ids == ''){
            return json(['code' => 100, 'msg' => '请选择要操作的数据']);
        }
        $lists = Db::name('match_apply_info')
            ->join('up_club', 'ma_club_id = club_id', 'left')
            ->join('up_match_group', 'mg_id = ma_group', 'left')
            ->join('up_match', 'ma_match_id = match_id', 'left')
            ->join('up__match_turns', 'mt_id = ma_turns_id', 'left')
            ->join('up__match_rounds', 'mr_id = ma_rounds_id', 'left')
            ->join('up_u_user', 'user_id = ma_user_id', 'left')
            ->field('ma_id, mg_name, ma_boat_type, match_start_time, ma_group_num, ma_dao_num, 
                ma_team_name, club_name, ma_match_id, mt_name, mr_name, user_phone, user_real_name')
            ->where('ma_id', 'IN', $ma_ids)
            ->order('ma_rounds_id asc, ma_dao_num asc')
            ->select();
        foreach($lists as $k => $v){
            // 队员
            $join_user = Db::table('up_match_apply_info')
                ->where('ma_team_name', $v['ma_team_name'])
                ->where('ma_match_id', $v['ma_match_id'])
                ->column('ma_user_name');
            $lists[$k]['ma_user_name'] = implode(',', $join_user);
            // 手机号
            $lists[$k]['user_phone'] = "\t".$v['user_phone']."\t";
            // 处理特殊字符串
            $user_name = json_encode($v['user_real_name']);
            $name = preg_replace("/\\\u[ed][0-9a-f]{3}\\\u[ed][0-9a-f]{3}/", "*", $user_name);//将特殊字符串替换成*
            $lists[$k]['user_real_name'] = json_decode($name);
        }
        
        $cellname = [
            ['user_real_name', '船长', 15, 'LEFT'],
            ['user_phone', '手机号', 15, 'LEFT'],
            ['mg_name', '组别', 15, 'LEFT'],
            ['mt_name', '轮次', 15, 'LEFT'],
            ['mr_name', '组次', 15, 'LEFT'],
            ['ma_dao_num', '道次', 15, 'LEFT'],
            ['ma_team_name', '队名', 15, 'LEFT'],
            ['ma_user_name', '队员', 15, 'LEFT']
        ];
        $res = exportExcel('秩序单','ZhiX',$cellname,$lists);
        return json($res);
    }
}